package com.epam.lunch.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import com.epam.lunch.domain.Address;
import com.epam.lunch.domain.Category;
import com.epam.lunch.domain.Food;
import com.epam.lunch.domain.Menu;
import com.epam.lunch.domain.Order;
import com.epam.lunch.domain.Restaurant;
import com.epam.lunch.service.JpaOrderRepository;
import com.epam.lunch.service.JpaRestaurantRepository;
import com.epam.lunch.service.SystemOutMenuLister;

public class JpaTest {

	private EntityManager manager;

	public JpaTest(EntityManager manager) {
		this.manager = manager;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
		EntityManager manager = factory.createEntityManager();
		JpaTest test = new JpaTest(manager);

		EntityTransaction tx = manager.getTransaction();
		tx.begin();
		try {
			test.initDB();
			JpaRestaurantRepository repo = new JpaRestaurantRepository(manager);
			SystemOutMenuLister lister = new SystemOutMenuLister(repo);
			
//			lister.listMenus();
//			test.listOrders();
			new JpaOrderRepository(manager).listAllOrders();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		tx.commit();

		System.out.println(".. done");
	}

	private void initDB() {
		if (getNumOfEntities("Restaurant") == 0) {
			System.out.println("### initializing DB");
			createAddress();
			createFood();
			createMenu();			
			createRestaurant();
		} else {
			System.out.println("### DB is OK");
		}
	}
	private void listRestaurants() {
		List<Restaurant> resultList = manager.createQuery("Select m From Restaurant m", Restaurant.class).getResultList();
		for (Restaurant next : resultList) {
			System.out.println("next menu: " + next);
		}

		
	}

	private void listMenu() {
		List<Menu> resultList = manager.createQuery("Select m From Menu m", Menu.class).getResultList();
		for (Menu next : resultList) {
			next.getFoods();
			System.out.println("next menu: " + next);
		}
	}

	private void createFood() {
			manager.persist(new Food("pacal", 670, "light soup", Category.DISHES));
			manager.persist(new Food("babgulyas", 850, "creates some gas", Category.SOUP));
			manager.persist(new Food("pangalaktikus gegepukkaszto", 1200, "hard stuff", Category.SHOTS));		

			manager.persist(new Food("tokfozelek", 500, "i necer liked it", Category.DISHES));
			manager.persist(new Food("brassoi", 1100, "creates some gas", Category.DISHES));
			manager.persist(new Food("kola", 300, "no description needed!", Category.SOFT_DRINK));
			manager.persist(new Food("cekla", 300, "boborjan's favourite", Category.DISHES));
			manager.persist(new Food("kave", 250, "100% arabica", Category.DISHES));
			manager.persist(new Food("ceklas kola", 350, "strange taste", Category.DISHES));
}
	
	private void createMenu() {
			List<Food> foods = manager.createQuery("Select a From Food a", Food.class).getResultList();
			Menu menu1 = new Menu();
			Menu menu2 = new Menu();
			menu1.setWeek(1);
			menu2.setWeek(1);
			manager.persist(menu1);
			manager.persist(menu2);
	
			int counter = 0;
			for (Food food : foods) {
				if (counter++ < 3) {
					food.setMenu(menu1);					
				} else {
					food.setMenu(menu2);
				}
				manager.persist(food);
			}
	}

	private void createRestaurant() {
		Menu firstMenu = manager.createQuery("Select a From Menu a", Menu.class).getResultList().get(0);
		Address firstAddress = manager.createQuery("Select a From Address a", Address.class).getResultList().get(0);
		Menu secondMenu = manager.createQuery("Select a From Menu a", Menu.class).getResultList().get(1);
		Address secondAddress = manager.createQuery("Select a From Address a", Address.class).getResultList().get(1);
			
		Restaurant rest1 = new Restaurant();
		rest1.setName("Ancsa");
		rest1.setMenu(firstMenu);
		rest1.setAddress(firstAddress);

		Restaurant rest2 = new Restaurant();
		rest2.setName("TenMinutes");
		rest2.setMenu(secondMenu);
		rest2.setAddress(secondAddress);

		manager.persist(rest1);
		manager.persist(rest2);
	}

	private void listFood() {
		List<Food> resultList = manager.createQuery("Select a From Food a", Food.class).getResultList();
		for (Food next : resultList) {
			System.out.println("next food: " + next);
		}

	}
	


	private void createAddress() {		
		manager.persist(new Address("futo 1.", "bp", "1081", "hungary"));
		manager.persist(new Address("futo 47.", "bp", "1082", "hungary"));
	}

	private int getNumOfEntities(String entityClass) {
		return manager.createQuery("Select x From " + entityClass + " x").getResultList().size();
	}

	private void listAddresses() {
		List<Address> resultList = manager.createQuery("Select a From Address a", Address.class).getResultList();
		for (Address next : resultList) {
			System.out.println("next address: " + next);
		}
	}

	private void listOrders() {
		List<Order> resultList = manager.createQuery("Select o From Order o", Order.class).getResultList();
		for (Order next : resultList) {
			System.out.println("next Order: " + next);
		}
		
	}

}
